package com.module.msg.view

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.module.app.base.BaseListFragment
import com.module.app.bean.ListViewBean
import com.module.base.base.adapter.BaseAdapter
import com.module.base.base.adapter.BaseNewAdapter
import com.module.base.base.adapter.BaseNewAdapter.OnItemOtherClickListener
import com.module.base.utils.CollectionUtil
import com.module.base.utils.DisplayUtils
import com.module.base.utils.ScrollUtils
import com.module.base.utils.ToastUtils
import com.module.msg.R
import com.module.msg.adapter.holder.MsgHolder
import com.module.msg.bean.MsgBean
import com.module.msg.databinding.MsgFragMsgHotBinding
import com.module.msg.model.HotMsgViewModel
import com.module.msg.utils.startMsg


class HotMsgFragment : BaseListFragment<HotMsgViewModel, MsgFragMsgHotBinding, BaseAdapter>() {

    private val headerView: View by lazy {
        LayoutInflater.from(context).inflate(R.layout.app_header_empty, null, false)
    }

    override fun bingViewModel() {
        mDatabind.vm = mViewModel
    }

    override fun createAdapter(): BaseAdapter {
        return object : BaseAdapter() {
            override fun onCreateDefaultViewHolder(
                parent: ViewGroup,
                viewType: Int
            ): MsgHolder {
                return MsgHolder(parent)
            }
        }
    }

    override fun onLazyLoad() {
        reload()
    }

    override fun initView() {
        super.initView()
        ScrollUtils(context, mDatabind.ivAdd, recyclerview)
        headerView.setPadding(
            headerView.paddingLeft,
            DisplayUtils.dip2px(5f),
            headerView.paddingRight,
            headerView.paddingBottom
        )
    }

    override fun initListener() {
        super.initListener()
        adapter.setOnItemClickListener(BaseNewAdapter.OnItemClickListener<MsgHolder, MsgBean> { _, bean ->
            context.startMsg(bean)
        })
        mDatabind.ivAdd.setOnClickListener {
            context.startMsg(null)
        }
        adapter.setOnItemOtherClickListener { holder, view, bean ->
            ToastUtils.showShort("跳转类型：$")
        }
        adapter.setOnItemOtherClickListener(OnItemOtherClickListener<MsgHolder?, MsgBean> { holder, view, bean ->
            when {
                R.id.tv_support === view.id -> {
                    ToastUtils.showShort("赞成")
                }
                R.id.tv_oppose === view.id -> {
                    ToastUtils.showShort("反对")
                }
                R.id.tv_jump === view.id -> {
                    ToastUtils.showShort("跳转")
                }
            }
        })
    }


    override fun onListLoadPage(
        isPull: Boolean,
        isRefresh: Boolean,
        page_no: Int,
        page_size: Int
    ) {
        mViewModel.getData()
    }

    override fun setListData(bean: ListViewBean) {
        super.setListData(bean)
        if (bean.isRefresh && !CollectionUtil.isEmptyOrNull(bean.list)) {
            adapter.setHeaderView(headerView)
        }
    }
}